﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.Win32;
using NPOI.HPSF;
using NPOI.HSSF.UserModel;

namespace VoiceTestPlatform
{
    public class NetworkStatusViewModel : ViewModelBase
    {

        public DelegateCommand ResultToExcelCommand { get; set; }

        public NetworkStatusViewModel()
        {
            ResultToExcelCommand = new DelegateCommand(ResultToExcelCommandAction);
        }

        private void ResultToExcelCommandAction(object obj)
        {
            const string extension = "xls";
            var fileName = "网络状态" + DateTime.Now.ToString("yyyyMMddHHmmss");
            var dialog = new SaveFileDialog
            {
                DefaultExt = extension,
                Filter = string.Format("{1} files (*.{0})|*.{0}|All files (*.*)|*.*", extension, "Excel"),
                FilterIndex = 1,
                FileName = fileName,
            };
            if (dialog.ShowDialog() != true) return;
            try
            {
                using (var stream = dialog.OpenFile())
                {
                    // ExportExcelAll(stream, gridView);
                    var hssfworkbook = new HSSFWorkbook(); //创建文件
                    var dsi = PropertySetFactory.CreateDocumentSummaryInformation();
                    dsi.Company = "广东美的集团公司";
                    hssfworkbook.DocumentSummaryInformation = dsi;
                    var si = PropertySetFactory.CreateSummaryInformation();
                    si.Subject = "本文件由语音测试系统导出";
                    hssfworkbook.SummaryInformation = si;
                    var sheet1 = hssfworkbook.CreateSheet("Sheet1");

                    //创建表头
                    var headerRow = sheet1.CreateRow(0);
                    var dataColumns = new List<string>() { "时间", "状态"};
                    for (var i = 0; i < dataColumns.Count(); i++)
                    {
                        headerRow.CreateCell(i).SetCellValue(dataColumns[i]);
                    }

                    int dataindex = 1;
                    var rs = NetworkStatusManager.Records.ToList();
                    foreach (var item in rs)
                    {
                        var deRow = sheet1.CreateRow(dataindex);
                        dataindex++;
                        deRow.CreateCell(0).SetCellValue(item.TimeString);
                        deRow.CreateCell(1).SetCellValue(item.Status);
                       
                    }

                    //统计
                  
                    hssfworkbook.Write(stream); //写入文件流
                }
            }
            catch (IOException ex)
            {
                //MessageBox.Show(string.Format("文件\"{0}\"已打开，请关闭后再点击导出！\n{1}", dialog.FileName, ex.Message), "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
        }

        private ObservableCollection<NetworkStatusRecord> records;

        public ObservableCollection<NetworkStatusRecord> Records
        {
            get { return NetworkStatusManager.Records; }
            //set { records = value; RaisePropertyChanged("Records"); }
        }

      

        public string CurrentStatus
        {
            get { return NetworkStatusManager.NetworkStatus == null ? "未知" : NetworkStatusManager.NetworkStatus.Value ? "正常" : "断开"; }
          
        }
        
    }
}
